/*
 * @(#)EFBaseAction.java 1.0 11/01/18
 *
 * Copyright 2011 YiChang Ewolf Information Technology Co.,Ltd, All rights reserved.
 */

package cn.org.timespace.easyframe.action;

import java.io.IOException;
import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;

import cn.org.timespace.easyframe.model.permission.EfUser;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

/**
 * <p>
 * easyframe框架控制层的父类
 * </p>
 *
 * @version 1.0, 11/01/18
 * @since 1.0
 * @author 
 */

public abstract class EFBaseAction extends ActionSupport implements ModelDriven<EFBaseModel>,ServletRequestAware,ServletResponseAware {
	
	private static final long serialVersionUID = -3570742913919917139L;
	public Logger log = Logger.getLogger(this.getClass());
	
	private HttpServletRequest request = ServletActionContext.getRequest();
	private HttpServletResponse response = ServletActionContext.getResponse();
	
	/**
	 * 用于存储Report对象的KEY
	 */
	public static final String REPORT = "report";
	public static final String PAGEMSG = "pageMsg";
	
	/**
	 * 查询时，用于Request携带数据集的KEY
	 */
	public static final String VOLIST = "voList";
	
	public static final String LIST = "list";
	public static final String ADD = "add";
	public static final String UPDATE = "update";
	public static final String VIEW = "view";
	public static final String EDIT = "edit";
	public static final String RESULT = "result";
	public static final String SHOW = "show";
	
	public static final String RECEPTION_INDEX = "receptionIndex";//前台接待首页
	
	//删除
	public static final String DELETE="delete";
	
	public static final String SESSION_KEY = "efUserSession";
	public static final String CONTROL_POINT_KEY = "cpMap";
	public static final String TOIMPORT = "toImport";
	public static final String TOEXPORT = "toExport";
	
	// 模型驱动Bean
	private EFBaseModel efModel = new EFBaseModelImpl();
	
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}
	public void setServletResponse(HttpServletResponse response){
		this.response=response;
	}
	
	public void setEfModel(EFBaseModel efModel) {
		this.efModel = efModel;
	}

	public EFBaseAction() {
		super();
	}
	
	public EFBaseAction(EFBaseModel model) {
		setEfModel(model);
	}
	

	public EFBaseModel getModel() {
		return this.efModel;
	}
	
	protected HttpServletRequest getRequest(){
			return this.request;
	}
	
	protected HttpServletResponse getResponse(){
		return this.response;
	}
	
	/**
	 * 获取当前日期
	 * @return
	 */
	protected Date getCurDate(){
		return new Date();
	}
	
	/**
	 * 获取当前登录IP
	 * @return
	 */
	protected String getCurLogonIp(){
		return getRequest().getRemoteHost();
	}
	
	/**
	 * 获取存储在Session里面的对象
	 * @return
	 */
	protected EfUserSession getEfUserSession(){
		Map httpSession = null;
		try{
			httpSession = ActionContext.getContext().getSession();
			Object obj = httpSession.get(SESSION_KEY);
			if(obj != null && obj instanceof EfUserSession){
				return ((EfUserSession) obj);
			}else{
				request.setAttribute("open", "open");
				response.sendRedirect(request.getContextPath() + "/EnvCheck.jsp");
			}
		}catch(Exception e){
			try {
				request.setAttribute("open", "open");
				response.sendRedirect(request.getContextPath() + "/EnvCheck.jsp");
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
		
		
		return null;
	}
	
	protected String getUserId(){
		return getEfUserSession().getEfUser().getUserId();
	}
	protected String getUserAccount(){
		return getEfUserSession().getEfUser().getUserAccount();
	}
	protected EfUser getCurEfUser(){
		EfUser t = new EfUser();
		t.setUserId(getUserId());
		return t;
	}
	
}
